import dateSelector from './components/dateSelector.vue'
import {
  AddMobileContactData
} from '@/utils/common-api.js'
export default {
  components: {
    dateSelector
  },
  data() {
    return {
      type: 1,
      showsc: false,
      selectedDate: '', //页面显示查询时间
      oldTime: '', //旧时间
      carNumber: '总计99辆',
      week: ['日', '一', '二', '三', '四', '五', '六'],
      weekText: '',
      list: [],
      totle: 0,
      pageIndex: 1,
      loadStatus: '',
      searchValue: null,
      isChinese: false,
      searchMobile: null,
      searchKey: ''
    }
  },
  watch: {
    selectedDate() {
      let dates = new Date(this.selectedDate)
      this.weekText = this.week[dates.getDay()]
    }
  },
  onLoad() {
    this.selectedDate = this.$u.timeFormat(new Date().getTime(), 'yyyy-mm-dd')
    this.oldTime = this.$u.timeFormat(new Date().getTime(), 'yyyy-mm-dd')
    let dates = new Date(this.selectedDate)
    this.weekText = this.week[dates.getDay()]
  },
  filters: {
    filterTel(tel) {
      let reg = /^(\d{3})\d*(\d{4})$/
      return tel.replace(reg, '$1****$2')
    },
    cutString(str, len) {
      if (str.length * 2 <= len) {
        return str
      }
      let strlen = 0
      let s = ''
      for (let i = 0; i < str.length; i++) {
        s = s + str.charAt(i)
        if (str.charCodeAt(i) > 128) {
          strlen = strlen + 2
          if (strlen >= len) {
            return s.substring(0, s.length - 1) + '...'
          }
        } else {
          strlen = strlen + 1
          if (strlen >= len) {
            return s.substring(0, s.length - 2) + '...'
          }
        }
      }
      return s
    },
    formatStoreType(data) {
      let text = ''
      switch (Number(data)) {
      case 0:
        text = '排队中'
        break
      case 1:
        text = '等待技师确认'
        break
      case 2:
        text = '取消排队'
        break
      case 10:
        text = '施工中'
        break
      case 11:
        text = '暂停施工'
        break
      case 12:
        text = '完成(未取车)'
        break
      case 20:
        text = '完成'
        break
      case 30:
        text = '取消'
        break
      default:
        text = ''
        break
      }
      return text
    },
    serviceTypes(data) {
      let text = ''
      switch (Number(data)) {
      case 0:
        text = '客户端订单'
        break
      case 1:
        text = '技师保养订单'
        break
      case 2:
        text = '技师洗车订单'
        break
      case 3:
        text = '技师美容订单'
        break
      case 4:
        text = '技师轮胎订单'
        break
      case 5:
        text = '全车适配订单'
        break
      default:
        text = ''
        break
      }
      return text
    }
  },
  created() {

  },
  onShow() {
    this.pageIndex = 1
    this.list = []
    this.GetBookingManagementList()
  },
  onReachBottom() {
    if (this.loadStatus !== 'nomore') {
      this.GetBookingManagementList()
    }

  },
  methods: {
    returnTime(time) {
      if (time) {
        return time.split('T')[0] + ' ' + time.split('T')[1]
      }
    },
    phone(num) {
      uni.makePhoneCall({
        phoneNumber: num
      })
    },

    //聊天
    concat(dataObj) {
      AddMobileContactData(this.$store.state.userInfoDetail.store.storeId)
      // 传和我聊天人的信息
      this.$store.commit('news/createConversationActive', {
        user: dataObj.userName, //店铺名称
        img: dataObj.headportrait, //店铺头像
        lastTime: '', //
        userId: dataObj.accountid, //店铺id
        conversationID: 'C2C' + dataObj.account_id,
      })
      uni.navigateTo({
        url: '/pages_news/room/room?user=' + dataObj.userName
      })
    },
    more() {
      this.GetBookingManagementList()
    },
    moreStatus(status) {
      this.loadStatus = status
    },
    //预约列表
    async GetBookingManagementList() {
      this.loadStatus = 'loading'
      console.log(this.isChinese)
      if (this.isChinese) { //如果是中文手机号为nll
        this.searchMobile = null
      } else {
        this.searchValue = null
      }
      try {
        let data = {
          'pageIndex': this.pageIndex,
          'pageSize': 10,
          'storeId': this.$store.state.userInfoDetail.store.storeId,
          'date': this.selectedDate, //this.selectedDate
          'plateNumber': this.searchValue || null,
          'userMobile': this.searchMobile || null
        }
        const res = await this.$api.common.getBookingManagementList(data)
        this.list = [...this.list, ...res.data.list]
        this.totle = res.data.total
        this.carNumber = `总计${res.data.total}辆`
        if (res.data.total <= 10) {
          this.loadStatus = 'nomore'
        } else {
          this.moreStatus(res.data.list.length !== 0 ? 'loadmore' : 'nomore')
        }
        uni.hideLoading()
        if (res.data.list.length === 0) return false
        this.pageIndex += 1 // 当前页加1
      } catch (e) {
        //TODO handle the exception
      }
    },
    confirmsc(data) {
      this.selectedDate = this.$u.timeFormat(data, 'yyyy-mm-dd')
      this.list = []
      this.pageIndex = 1
      this.GetBookingManagementList()
      this.showsc = false
    },
    closeTime() {
      this.showsc = false
    },

    goPage(item) {
      uni.navigateTo({
        url: '/pages_common/bookingDetails/bookingDetails?id=' + item.orderId+'&arriveId='+item.arriveId+'&state='+item.arriveStatus
      })
    },
    onSearch(e) {
      if (/.*[\u4e00-\u9fa5]+.*$/.test(e)) {
        this.isChinese = true
      } else {
        this.isChinese = false

      }
      this.searchValue = e.trim()
      this.searchMobile = e.trim()
      this.list = []
      this.pageIndex = 1
      this.GetBookingManagementList()
    },
    onChange(e) {
      //搜索值为空
      if (!e) {
        this.searchMobile = null
        this.searchValue = null
        this.list = []
        this.pageIndex = 1
        this.GetBookingManagementList()
      }
    },
  }
}
